/**
 * 分类管理
 */
GlobalTypeManager = Ext.extend(Ext.Panel, {
	constructor : function(config) {
		Ext.applyIf(this, config);
		this.initUIComponents();
		GlobalTypeManager.superclass.constructor.call(this, {
					id : 'GlobalTypeManager',
					layout : 'border',
					title : '系统分类树管理',
					iconCls : 'menu-globalType',
					items : [this.leftPanel, this.centerPanel]
				});
	},
	initUIComponents : function() {
		this.topLeftPanel = new Ext.Panel({
			layout : 'fit',
			region : 'north',
			border : false,
			height : 23,
			items : [{
				xtype : 'combo',
				editable : false,
				id : 'comboGlobalType',
				name : 'comboGlobalTypeValue',
				displayField : 'name',
				valueField : 'key',
				queryMode : 'local',
				store : new Ext.data.ArrayStore({
					autoLoad: true,
					proxy: {
						type: 'ajax',
						url : __ctxPath + '/system/comboTypeKey.do'
					},
					fields : ['key', 'name']
				}),
				listeners : {
					scope : this,
					'select' : this.selectType
				}
			}]
		});

		// 分类树
		this.typeTree = new Ext.ux.TreePanelEditor({
			id : 'GlobalTypeTree',
			region : 'center',
			layout : 'fit',
			title : '分类树',
			split : true,
			border : false,
			autoScroll : true,
			url : __ctxPath + '/system/treeGlobalType.do?catKey=DP',
			onclick : function(view, node) {
				var parentId = node.id;
				var grid = Ext.getCmp('globalTypeCenGrid');
				if (grid != null) {
					var catKey = Ext.getCmp('comboGlobalType').getValue();
					var store = grid.getStore();
					store.proxy.url = __ctxPath + "/system/subGlobalType.do";
					store.proxy.extraParams = {
						parentId : parentId,
						catKey : catKey
					};
					store.reload();
				}
			},
			contextMenuItems : [{
				text : '新建分类',
				scope : this,
				iconCls : 'btn-add',
				handler : function() {
					// 类别不能为空
					var catKey = Ext.getCmp('comboGlobalType').getValue();
					if(catKey == null) {
						Ext.ux.Toast.msg('操作信息', '请先选择左侧下拉选项中的分类！');
						return;
					}
					
					var globalTypeTree = Ext.getCmp('GlobalTypeTree');
					var parentId = globalTypeTree.selectedNode.id;

					var globalTypeForm = new GlobalTypeForm({
						parentId : parentId,
						catKey : catKey,
						callback : function() {
							Ext.getCmp('GlobalTypeTree').store.reload();
							Ext.getCmp('globalTypeCenGrid').getStore().reload();
						}
					});
					globalTypeForm.show();
				}
			}, {
				text : '修改分类',
				scope : this,
				iconCls : 'btn-edit',
				handler : function() {
					var globalTypeTree = Ext.getCmp('GlobalTypeTree');
					var proTypeId = globalTypeTree.selectedNode.id;
					if(proTypeId == 0) {
						Ext.ux.Toast.msg('操作信息', '不能修改【总分类】！');
						return;						
					}

					var globalTypeForm = new GlobalTypeForm({
						proTypeId : proTypeId,
						callback : function() {
							Ext.getCmp('GlobalTypeTree').store.reload();
							Ext.getCmp('globalTypeCenGrid').getStore().reload();
						}
					});
					globalTypeForm.show();
				}
			}]
		});

		this.leftPanel = new Ext.Panel({
					region : 'west',
					title : '分类管理',
					layout : 'fit',
					collapsible : true,
					split : true,
					width : 200,
					tbar : new Ext.Toolbar({
								items : [{
									xtype : 'button',
									text : '新建类',
									iconCls : 'btn-add',
									handler : function() {
										new TypeKeyForm().show();
									}
								}]
							}),
					items : [{
								xtype : 'panel',
								border : false,
								layout : 'border',
								items : [this.topLeftPanel, this.typeTree]
							}]
				}

		);
		this.topbar = new Ext.Toolbar({
			items : [{
				text : '新建分类',
				iconCls : 'btn-add',
				handler : function() {
					// 类别不能为空
					var type = Ext.getCmp('comboGlobalType').getValue();
					if(type == null) {
						Ext.ux.Toast.msg('操作信息', '请先选择左侧下拉选项中的分类！');
						return;
					}
					
					var globalTypeTree = Ext.getCmp('GlobalTypeTree');
					var selectedNode = globalTypeTree.selectedNode;

					if (!selectedNode) {
						Ext.ux.Toast.msg('操作信息', '请选择左树中的分类！');
						return;
					}
					var catKey = Ext.getCmp('comboGlobalType').getValue();

					var globalTypeForm = new GlobalTypeForm({
						parentId : selectedNode.id,
						catKey : catKey,
						callback : function() {
							Ext.getCmp('GlobalTypeTree').store.reload();
							Ext.getCmp('globalTypeCenGrid').getStore().reload();
						}
					});
					globalTypeForm.show();
				}
			}, '-', {
				xtype : 'button',
				text : '保存',
				iconCls : 'btn-save',
				scope : this,
				handler : function() {
					var grid = this.centerPanel;
					var store = grid.getStore();
					var params = [];

					for (var i = 0; i < store.getCount(); i += 1) {
						var record = store.getAt(i);
						params.push(record.data);
					}

					Ext.Ajax.request({
								method : 'post',
								url : __ctxPath
										+ '/system/mulSaveGlobalType.do',
								params : {
									data : Ext.encode(params)
								},
								success : function(request) {
									Ext.ux.Toast.msg('操作信息', '成功设置');
									store.reload();
									grid.getView().refresh();
									Ext.getCmp('GlobalTypeTree').store.reload();
								},
								failure : function(request) {
									Ext.ux.Toast.msg('操作信息', '设置出错，请联系管理员!');
								}
							});

				}
			}, {
				text : '删除',
				iconCls : 'btn-del',
				scope : this,
				handler : function() {
					var gridPanel = this.centerPanel;
					var selectRecords = gridPanel.getSelectionModel().getSelection();
					if (selectRecords.length == 0) {
						Ext.ux.Toast.msg("信息", "请选择要删除的记录！");
						return;
					}
					var ids = Array();
					for (var i = 0; i < selectRecords.length; i++) {
						ids.push(selectRecords[i].data.proTypeId);
					}

					Ext.Msg.confirm('信息确认', '您确认要删除所选记录吗？', function(btn) {
						if (btn == 'yes') {
							Ext.Ajax.request({
										url : __ctxPath
												+ '/system/multiDelGlobalType.do',
										params : {
											ids : ids
										},
										method : 'POST',
										success : function(response, options) {
											Ext.ux.Toast.msg('操作信息',
													'成功删除该分类！');
											gridPanel.getStore().reload();
											Ext.getCmp('GlobalTypeTree').store.reload();
										},
										failure : function(response, options) {
											Ext.ux.Toast.msg('操作信息', '操作出错，请联系管理员！');
										}
									});
						}
					});

				}
			}]
		});
		this.centerPanel = new KE.GridPanel({
			region : 'center',
			title : '分类列表',
			tbar : this.topbar,
			id : 'globalTypeCenGrid',
			url : __ctxPath + "/system/subGlobalType.do",
			fields : ['proTypeId', 'typeName', 'nodeKey', 'sn'],
			baseParams : {
				parentId : 0
			},
			columns : [{
						header : 'proTypeId',
						dataIndex : 'proTypeId',
						hidden : true
					}, {
						header : '名称',
						dataIndex : 'typeName',
						editor : new Ext.form.TextField({
									allowBlank : false
								})
					}, {
						header : '节点Key',
						dataIndex : 'nodeKey',
						editor : new Ext.form.TextField({
									allowBlank : false
								})
					}, {
						header : '序号',
						dataIndex : 'sn'
					}, {
						text : '管理',
						xtype : 'actioncolumn',
						width : 80,
						items : [{
									iconCls : 'btn-last',
									tooltip : '向下',
									scope: this,
									handler : this.toDown
								}, {
									iconCls : 'btn-up',
									tooltip : '向上',
									scope: this,
									handler : this.toUp
								}]
					}]
		});

	},// end of initUIComponents
	// 向上
	toUp: function(grid, row) {
		var grid = Ext.getCmp('globalTypeCenGrid');
		var store = grid.getStore();
		if (row == 0) {
			Ext.ux.Toast.msg('操作信息', '已经为第一条!');
			return;
		}
		var rd1 = store.getAt(row - 1);
		var rd2 = store.getAt(row);
		store.removeAt(row);
		store.removeAt(row - 1);
		store.insert(row - 1, rd2);
		store.insert(row, rd1);
	},
	// 向下
	toDown: function(grid, row) {
		var grid = Ext.getCmp('globalTypeCenGrid');
		var store = grid.getStore();
		if (row == store.getCount() - 1) {
			Ext.ux.Toast.msg('操作信息', '已经为最后一条!');
			return;
		}
		var rd1 = store.getAt(row);
		var rd2 = store.getAt(row + 1);

		store.removeAt(row + 1);
		store.removeAt(row);

		store.insert(row, rd2);
		store.insert(row + 1, rd1);
	},
	// 下拉选择框触发
	selectType: function(combo, record, index) {
		var catKey = combo.getValue();
		var panelTree = Ext.getCmp('GlobalTypeTree');
		// 加载树
		panelTree.store.proxy.url = __ctxPath + '/system/treeGlobalType.do',
		panelTree.store.proxy.extraParams = {catKey : catKey}
		
		panelTree.selectedNode = null;
		panelTree.store.reload();
		var grid = Ext.getCmp('globalTypeCenGrid');
		if (grid != null) {
			var store = grid.getStore();
			store.proxy.url = __ctxPath + "/system/subGlobalType.do";
			store.proxy.extraParams = {
				parentId : 0,
				catKey : catKey
			};
			store.reload();
		}
	}
});